package com.appspot.menox3secandroid2011.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slim3.datastore.Datastore;
import org.slim3.datastore.EqualCriterion;

import com.appspot.menox3secandroid2011.meta.NYUTAIRIREKI_TBLMeta;
import com.appspot.menox3secandroid2011.meta.GENBA_MSTMeta;
import com.appspot.menox3secandroid2011.meta.COMPANY_MSTMeta;
import com.appspot.menox3secandroid2011.meta.SAGYOSYA_MSTMeta;

import com.appspot.menox3secandroid2011.model.NYUTAIRIREKI_TBL;
import com.appspot.menox3secandroid2011.model.GENBA_MST;
import com.appspot.menox3secandroid2011.model.COMPANY_MST;
import com.appspot.menox3secandroid2011.model.SAGYOSYA_MST;

import com.google.appengine.api.datastore.Query.SortDirection;


public class NyutaiRirekiItiran {

    public List<NYUTAIRIREKI_TBL> getRirekiView(String genbaCode, String companyCode, String sagyosyaId, String kbn, Date time){

        NYUTAIRIREKI_TBLMeta check = NYUTAIRIREKI_TBLMeta.get();

        List<EqualCriterion> filterList = new ArrayList<EqualCriterion>();
        if(genbaCode != null && genbaCode.length() > 0){
            filterList.add(check.genbacode.equal(genbaCode));
        }
        if(companyCode != null && companyCode.length() > 0){
            filterList.add(check.kaisyacode.equal(companyCode));
        }
        if(sagyosyaId != null && sagyosyaId.length() > 0){
            filterList.add(check.sagyousyaid.equal(sagyosyaId));
        }

        EqualCriterion[] filter = new EqualCriterion[filterList.size()];
        for(int i=0; i<filter.length; i++){
            filter[i] = filterList.get(i);
        }

        List<NYUTAIRIREKI_TBL> list = Datastore.query(check)
            .filter(filter)
            .sort("nyutaijotime", SortDirection.DESCENDING)
            .asList();
        return list;
    }

    public List<GENBA_MST> getGenbaView(String genbaCode){

        GENBA_MSTMeta check = GENBA_MSTMeta.get();
        List<GENBA_MST> list = Datastore.query(check)
            .sort("genbacode", SortDirection.ASCENDING)
            .asList();
        return list;
    }

    public List<COMPANY_MST> getCompanyView(String kaisyaCode){

        COMPANY_MSTMeta check = COMPANY_MSTMeta.get();
        List<COMPANY_MST> list = Datastore.query(check)
            .sort("kaisyacode", SortDirection.ASCENDING)
            .asList();
        return list;
    }

    public List<SAGYOSYA_MST> getSagyosyaView(String sagyousyaId){

        SAGYOSYA_MSTMeta check = SAGYOSYA_MSTMeta.get();
        List<SAGYOSYA_MST> list = Datastore.query(check)
            .sort("SagyousyaID", SortDirection.ASCENDING)
            .asList();

        return list;
    }

}
